package org.dizena.modules.master.dao;

import org.dizena.common.mongo.CommonRepository;
import org.dizena.common.mongo.PageQuery;
import org.dizena.modules.master.bean.KvConfig;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class KvConfigDao extends CommonRepository<KvConfig> {
    @Resource
    private MongoTemplate mt;

    @Override
    public KvConfig insert(KvConfig m) {
        return mt.insert(m);
    }

    @Override
    public void delete(String id) {
        mt.remove(idQuery(id), KvConfig.class);
    }

    @Override
    public void delete(String key, Object value) {
        mt.remove(kvQuery(key, value), KvConfig.class);
    }

    @Override
    public void delete(String[] keys, Object[] values) {
        mt.remove(kvQuery(keys, values), KvConfig.class);
    }

    @Override
    public void update(KvConfig m) {
        mt.updateFirst(idQuery(m.getId()), mongoUpdate(m), KvConfig.class);
    }

    @Override
    public KvConfig findOne(String id) {
        return mt.findOne(idQuery(id), KvConfig.class);
    }

    @Override
    public KvConfig findOne(String key, Object value) {
        return mt.findOne(kvQuery(key, value), KvConfig.class);
    }

    @Override
    public KvConfig findOne(String[] keys, Object[] values) {
        return mt.findOne(kvQuery(keys, values), KvConfig.class);
    }

    @Override
    public long count(String key, Object value) {
        return mt.count(kvQuery(key, value), KvConfig.class);
    }

    @Override
    public long count(String[] keys, Object[] values) {
        return mt.count(kvQuery(keys, values), KvConfig.class);
    }

    @Override
    public List<KvConfig> findMany(String key, Object value) {
        return mt.find(kvQuery(key, value), KvConfig.class);
    }

    @Override
    public List<KvConfig> findMany(String[] keys, Object[] values) {
        return mt.find(kvQuery(keys, values), KvConfig.class);
    }

    @Override
    public List<KvConfig> find(PageQuery pageQuery) {
        return mt.find(pageQuery(pageQuery), KvConfig.class);
    }

    @Override
    public long count(PageQuery pageQuery) {
        return mt.count(countQuery(pageQuery), KvConfig.class);
    }

    public void deleteByKey(String key) {
        mt.remove(kvQuery("key", key), KvConfig.class);
    }
}
